package Service.Impl;

import Bean.Cart;
import Bean.Furn;
import Bean.Page;
import Dao.FurnDao;
import Dao.Impl.FurnDaoImpl;
import Service.FurnService;
import Utils.DBHelp;
import javaWeb.BasicServlet;

import java.math.BigDecimal;
import java.math.MathContext;
import java.util.ArrayList;
import java.util.List;

public class FurnServiceImpl implements FurnService {
    public static void main(String[] args) {

    }
    private FurnDao furnDao = new FurnDaoImpl();
    @Override
    public boolean insertFurn(Furn furn) {
        Furn one = furnDao.getFurn(furn.getFurnName(), furn.getMerchant());
        if (one != null){
            return false;
        }
        return furnDao.InsertFurn(furn);
    }
    @Override
    public List<Furn> getAllFurn() {
        return furnDao.getAllFurn();
    }
    @Override
    public boolean deleteFurn(String FurnName,String Merchant) {
        return furnDao.deleteFurn(FurnName,Merchant);
    }

    @Override
    public boolean updateFurn(Integer id, Furn furn) {
        return furnDao.updateFurn(id,furn);
    }

    @Override
    public Furn getFurn(String sql,Object... nums) {
        return furnDao.getOneFurn(sql,nums);
    }
    public Integer getCount(String sql) {
        return furnDao.getCount(sql);
    }
    @Override
    public Page initManagePage(Page page,int ManagePageSize){
        String sql = "select  count(*) from furn;";
        int sum = getCount(sql);
        //多少条记录
        page.setSum(sum);
        // 多个为一组
        page.setNumberSize(ManagePageSize);

        page.setBeginIndex(1);
        int PageCount = 0;
        if (sum % ManagePageSize == 0){
            PageCount = sum / ManagePageSize;
        }else {
            PageCount = sum/ManagePageSize+1;
        }
        // 一共有多少组
        page.setMaxSize(PageCount);
        return page;
    }

    @Override
    public List<Furn> getSomeFurn(Page page) {
        String sql ="select  * from furn limit ? offset ?;";
        List<Furn> someFurn = furnDao.getSomeFurn(sql,page);
        return someFurn;
    }

    @Override
    public Page<Furn> getPage_Furn(int begin, int size) {
        return furnDao.getPage(begin,size);
    }

    @Override
    public Page<Furn> Search(String keyWord,int begin,int Size) {
        Page<Furn> furnPage = new Page<>();
        furnPage.setKeyWord(keyWord);
        String im = "%"+keyWord+"%";
        String sql = "select count(*) from furn where FurnName like ?;";
        Integer count = furnDao.getCount(sql, im);
        int PageSize = 0;
        if (count %Size == 0){
            PageSize = count % Size;
        }else {
            PageSize = count % Size +1;
        }
        furnPage.setSum(count);
        furnPage.setBeginIndex(begin);
        furnPage.setNumberSize(Size);
        furnPage.setMaxSize(PageSize);
        furnPage.setItem(furnDao.Search(im, begin, Size));
        return furnPage;
    }
    @Override
    public List<Furn> setGoods(List<Cart.CartGood> cartGoodList) {
        List<Furn> list = new ArrayList<>();
        String sql = "select id, FurnName, Merchant, Price, Volume, inventory, img_path imgPath from Furn where id = ?";
        for (Cart.CartGood good : cartGoodList) {
            Furn oneFurn = furnDao.getOneFurn(sql, good.FurnId);
            list.add(oneFurn);
        }
        return list;
    }

    @Override
    public BigDecimal calculatePrice(List<Cart.CartGood> list) {
        BigDecimal bigDecimal = new BigDecimal(0);
        String sql = "select id, FurnName, Merchant, Price, Volume, inventory, img_path imgPath from Furn where id = ?";
        MathContext mc = new MathContext(3, MathContext.DECIMAL32.getRoundingMode());
        for (Cart.CartGood good :list) {
            Furn oneFurn = furnDao.getOneFurn(sql, good.FurnId);
            BigDecimal bigDecimal1 = new BigDecimal(good.amount);
            bigDecimal1 = bigDecimal1.multiply(oneFurn.getPrice(),mc);
            bigDecimal = bigDecimal.add(bigDecimal1);
        }
        return bigDecimal;
    }
    @Override
    public BigDecimal calculatePrice(Integer userId) {
        CartServiceImpl cartService = new CartServiceImpl();
        List<Cart.CartGood> allCartFurn = cartService.getAllCartFurn(userId);
        String sql = "select id, FurnName, Merchant, Price, Volume, inventory, img_path imgPath from Furn where id = ?";
        MathContext mc = new MathContext(3, MathContext.DECIMAL32.getRoundingMode());
        BigDecimal bigDecimal = new BigDecimal(0);
        for (Cart.CartGood good :allCartFurn) {
            Furn oneFurn = furnDao.getOneFurn(sql, good.FurnId);
            BigDecimal bigDecimal1 = new BigDecimal(good.amount);
            bigDecimal1 = bigDecimal1.multiply(oneFurn.getPrice(),mc);
            bigDecimal = bigDecimal.add(bigDecimal1);
        }
        return bigDecimal;
    }
}
